home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / docastle.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  9KB  |  346 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7.   (Cocktail mode flipscreen implemented by Chad Hendrickson Aug 1, 1999)
  8.  
  9. ***************************************************************************/
  10.  
  11. #include "driver.h"
  12. #include "vidhrdw/generic.h"
  13.  
  14.  
  15.  
  16. static struct osd_bitmap *tmpbitmap1;
  17. static char sprite_transparency[256];
  18. static int flipscreen = 0;
  19.  
  20.  
  21. /***************************************************************************
  22.  
  23.   Convert the color PROMs into a more useable format.
  24.  
  25.   Mr. Do's Castle / Wild Ride / Run Run have a 256 bytes palette PROM which
  26.   is connected to the RGB output this way:
  27.  
  28.   bit 7 -- 200 ohm resistor  -- RED
  29.         -- 390 ohm resistor  -- RED
  30.         -- 820 ohm resistor  -- RED
  31.         -- 200 ohm resistor  -- GREEN
  32.         -- 390 ohm resistor  -- GREEN
  33.         -- 820 ohm resistor  -- GREEN
  34.         -- 200 ohm resistor  -- BLUE
  35.   bit 0 -- 390 ohm resistor  -- BLUE
  36.  
  37. ***************************************************************************/
  38. static void convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom,
  39.         int priority)
  40. {
  41.     int i,j;
  42.     #define TOTAL_COLORS(gfxn) (Machine->gfx[gfxn]->total_colors * Machine->gfx[gfxn]->color_granularity)
  43.     #define COLOR(gfxn,offs) (colortable[Machine->drv->gfxdecodeinfo[gfxn].color_codes_start + offs])
  44.  
  45.  
  46.     for (i = 0;i < 256;i++)
  47.     {
  48.         int bit0,bit1,bit2;
  49.  
  50.  
  51.         /* red component */
  52.         bit0 = (*color_prom >> 5) & 0x01;
  53.         bit1 = (*color_prom >> 6) & 0x01;
  54.         bit2 = (*color_prom >> 7) & 0x01;
  55.         *(palette++) = 0x23 * bit0 + 0x4b * bit1 + 0x91 * bit2;
  56.         /* green component */
  57.         bit0 = (*color_prom >> 2) & 0x01;
  58.         bit1 = (*color_prom >> 3) & 0x01;
  59.         bit2 = (*color_prom >> 4) & 0x01;
  60.         *(palette++) = 0x23 * bit0 + 0x4b * bit1 + 0x91 * bit2;
  61.         /* blue component */
  62.         bit0 = 0;
  63.         bit1 = (*color_prom >> 0) & 0x01;
  64.         bit2 = (*color_prom >> 1) & 0x01;
  65.         *(palette++) = 0x23 * bit0 + 0x4b * bit1 + 0x91 * bit2;
  66.  
  67.         color_prom++;
  68.     }
  69.  
  70.     /* reserve one color for the transparent pen (none of the game colors can have */
  71.     /* these RGB components) */
  72.     *(palette++) = 1;
  73.     *(palette++) = 1;
  74.     *(palette++) = 1;
  75.     /* and the last color for the sprite covering pen */
  76.     *(palette++) = 2;
  77.     *(palette++) = 2;
  78.     *(palette++) = 2;
  79.  
  80.  
  81.     /* characters */
  82.     /* characters have 4 bitplanes, but they actually have only 8 colors. The fourth */
  83.     /* plane is used to select priority over sprites. The meaning of the high bit is */
  84.     /* reversed in Do's Castle wrt the other games. */
  85.  
  86.     /* first create a table with all colors, used to draw the background */
  87.     for (i = 0;i < 32;i++)
  88.     {
  89.         for (j = 0;j < 8;j++)
  90.         {
  91.             colortable[16*i+j] = 8*i+j;
  92.             colortable[16*i+j+8] = 8*i+j;
  93.         }
  94.     }
  95.     /* now create a table with only the colors which have priority over sprites, used */
  96.     /* to draw the foreground. */
  97.     for (i = 0;i < 32;i++)
  98.     {
  99.         for (j = 0;j < 8;j++)
  100.         {
  101.             if (priority == 0)    /* Do's Castle */
  102.             {
  103.                 colortable[32*16+16*i+j] = 256;    /* high bit clear means less priority than sprites */
  104.                 colortable[32*16+16*i+j+8] = 8*i+j;
  105.             }
  106.             else    /* Do Wild Ride, Do Run Run, Kick Rider */
  107.             {
  108.                 colortable[32*16+16*i+j] = 8*i+j;
  109.                 colortable[32*16+16*i+j+8] = 256;    /* high bit set means less priority than sprites */
  110.             }
  111.         }
  112.     }
  113.  
  114.     /* sprites */
  115.     /* sprites have 4 bitplanes, but they actually have only 8 colors. The fourth */
  116.     /* plane is used for transparency. */
  117.     for (i = 0;i < 32;i++)
  118.     {
  119.         for (j = 0;j < 8;j++)
  120.         {
  121.             colortable[64*16+16*i+j] = 256;    /* high bit clear means transparent */
  122.             if (j != 7)
  123.                 colortable[64*16+16*i+j+8] = 8*i+j;
  124.             else
  125.                 colortable[64*16+16*i+j+8] = 257;    /* sprite covering color */
  126.         }
  127.     }
  128.  
  129.  
  130.    /* now check our sprites and mark which ones have color 15 ('draw under') */
  131.     {
  132.         struct GfxElement *gfx;
  133.         int x,y;
  134.         unsigned char *dp;
  135.  
  136.         gfx = Machine->gfx[1];
  137.         for (i=0;i<gfx->total_elements;i++)
  138.         {
  139.             sprite_transparency[i] = 0;
  140.  
  141.             dp = gfx->gfxdata + i * gfx->char_modulo;
  142.             for (y=0;y<gfx->height;y++)
  143.             {
  144.                 for (x=0;x<gfx->width;x++)
  145.                 {
  146.                     if (dp[x] == 15)
  147.                         sprite_transparency[i] = 1;
  148.                 }
  149.                 dp += gfx->line_modulo;
  150.             }
  151.  
  152.             if (sprite_transparency[i])
  153. logerror("sprite %i has transparency.\n",i);
  154.         }
  155.     }
  156. }
  157.  
  158.  
  159.  
  160. void docastle_vh_convert_color_prom(unsigned char *palette,unsigned short *colortable,const unsigned char *color_prom)
  161. {
  162.     convert_color_prom(palette,colortable,color_prom,0);
  163. }
  164.  
  165. void dorunrun_vh_convert_color_prom(unsigned char *palette,unsigned short *colortable,const unsigned char *color_prom)
  166. {
  167.     convert_color_prom(palette,colortable,color_prom,1);
  168. }
  169.  
  170.  
  171.  
  172. /***************************************************************************
  173.  
  174.   Start the video hardware emulation.
  175.  
  176. ***************************************************************************/
  177. int docastle_vh_start(void)
  178. {
  179.     if (generic_vh_start() != 0)
  180.         return 1;
  181.  
  182.     if ((tmpbitmap1 = osd_create_bitmap(Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
  183.     {
  184.         generic_vh_stop();
  185.         return 1;
  186.     }
  187.  
  188.     return 0;
  189. }
  190.  
  191.  
  192.  
  193. /***************************************************************************
  194.  
  195.   Stop the video hardware emulation.
  196.  
  197. ***************************************************************************/
  198. void docastle_vh_stop(void)
  199. {
  200.     osd_free_bitmap(tmpbitmap1);
  201.     generic_vh_stop();
  202. }
  203.  
  204.  
  205. static void setflip(int flip)
  206. {
  207.     if (flipscreen != flip)
  208.     {
  209.         flipscreen = flip;
  210.         memset(dirtybuffer,1,videoram_size);
  211.     }
  212. }
  213.  
  214. READ_HANDLER( docastle_flipscreen_off_r )
  215. {
  216.     setflip(0);
  217.     return 0;
  218. }
  219.  
  220. READ_HANDLER( docastle_flipscreen_on_r )
  221. {
  222.     setflip(1);
  223.     return 0;
  224. }
  225.  
  226. WRITE_HANDLER( docastle_flipscreen_off_w )
  227. {
  228.     setflip(0);
  229. }
  230.  
  231. WRITE_HANDLER( docastle_flipscreen_on_w )
  232. {
  233.     setflip(1);
  234. }
  235.  
  236.  
  237.  
  238. /***************************************************************************
  239.  
  240.   Draw the game screen in the given osd_bitmap.
  241.   Do NOT call osd_update_display() from this function, it will be called by
  242.   the main emulation engine.
  243.  
  244. ***************************************************************************/
  245. void docastle_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  246. {
  247.     int offs;
  248.  
  249.  
  250.     /* for every character in the Video RAM, check if it has been modified */
  251.     /* since last time and update it accordingly. */
  252.     for (offs = videoram_size - 1;offs >= 0;offs--)
  253.     {
  254.         if (dirtybuffer[offs])
  255.         {
  256.             int sx,sy;
  257.  
  258.  
  259.             dirtybuffer[offs] = 0;
  260.  
  261.             sx = offs % 32;
  262.             sy = offs / 32;
  263.  
  264.             if (flipscreen)
  265.             {
  266.                 sx = 31 - sx;
  267.                 sy = 31 - sy;
  268.             }
  269.  
  270.             drawgfx(tmpbitmap,Machine->gfx[0],
  271.                     videoram[offs] + 8*(colorram[offs] & 0x20),
  272.                     colorram[offs] & 0x1f,
  273.                     flipscreen,flipscreen,
  274.                     8*sx,8*sy,
  275.                     &Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  276.  
  277.             /* also draw the part of the character which has priority over the */
  278.             /* sprites in another bitmap */
  279.             drawgfx(tmpbitmap1,Machine->gfx[0],
  280.                     videoram[offs] + 8*(colorram[offs] & 0x20),
  281.                     32 + (colorram[offs] & 0x1f),
  282.                     flipscreen,flipscreen,
  283.                     8*sx,8*sy,
  284.                     &Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  285.         }
  286.     }
  287.  
  288.  
  289.     /* copy the character mapped graphics */
  290.     copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  291.  
  292.  
  293.     /* Draw the sprites. Note that it is important to draw them exactly in this */
  294.     /* order, to have the correct priorities. */
  295.     for (offs = 0;offs < spriteram_size;offs += 4)
  296.     {
  297.         int sx,sy,flipx,flipy,code,color;
  298.  
  299.  
  300.         code = spriteram[offs + 3];
  301.         color = spriteram[offs + 2] & 0x1f;
  302.         sx = spriteram[offs + 1];
  303.         sy = spriteram[offs];
  304.         flipx = spriteram[offs + 2] & 0x40;
  305.         flipy = spriteram[offs + 2] & 0x80;
  306.  
  307.  
  308.         if (flipscreen)
  309.         {
  310.             sx = 240 - sx;
  311.             sy = 240 - sy;
  312.             flipx = !flipx;
  313.             flipy = !flipy;
  314.         }
  315.  
  316.         drawgfx(bitmap,Machine->gfx[1],
  317.                 code,
  318.                 color,
  319.                 flipx,flipy,
  320.                 sx,sy,
  321.                 &Machine->drv->visible_area,TRANSPARENCY_COLOR,256);
  322.  
  323.  
  324.         /* sprites use color 0 for background pen and 8 for the 'under tile' pen.
  325.            The color 7 is used to cover over other sprites.
  326.  
  327.            At the beginning we scanned all sprites and marked the ones that contained
  328.            at least one pixel of color 7, so we only need to worry about these few. */
  329.         if (sprite_transparency[code])
  330.         {
  331.             struct rectangle clip;
  332.  
  333.             clip.min_x = sx;
  334.             clip.max_x = sx+31;
  335.             clip.min_y = sy;
  336.             clip.max_y = sy+31;
  337.  
  338.             copybitmap(bitmap,tmpbitmap,0,0,0,0,&clip,TRANSPARENCY_THROUGH,Machine->pens[257]);
  339.         }
  340.     }
  341.  
  342.  
  343.     /* now redraw the portions of the background which have priority over sprites */
  344.     copybitmap(bitmap,tmpbitmap1,0,0,0,0,&Machine->drv->visible_area,TRANSPARENCY_COLOR,256);
  345. }
  346.